using FlowCanvas;
using FlowCanvas.Nodes;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    public class Switch2BehaviorCamera : CallableActionNodeBase
    {
        [HideInInspector]
        private FlowOutput onSwitch;
        [HideInInspector]
        private string cName;
        
        public void Invoke(string cameraName)
        {
            cName = cameraName;
            BlueprintUtils.Switch2BehaviorCamera(cameraName,OnSwitchCamera);
        }

        private void OnSwitchCamera(BehaviorCamera behaviorCamera)
        {
            if (behaviorCamera.cameraName == cName)
            {
                onSwitch?.Call(new Flow());
            }
        }

        protected override void OnRegisterPorts(FlowNode node)
        {
            var o = node.AddFlowOutput("In");
            var p1 = node.AddValueInput<string>("cameraName");
            onSwitch = node.AddFlowOutput("onSwitch");
            
            node.AddFlowInput("Out", (Flow f) => { Invoke(p1.value); o.Call(f); });
        }
    }
}